/* Copyright (C) 2007-2008  klevgert  http://code.google.com/p/db4o-netbeans/

This file is part of the db4o-netbeans module suite.

db4o-netbeans is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation.

db4o-netbeans is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */

package com.db4o.nb.options;

import com.db4o.nb.util.ExtensionFileFilter;
import com.db4o.nb.util.PreferencesUtil;
import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.ListModel;

final class Db4oPanel extends javax.swing.JPanel {
  
  private final Db4oOptionsPanelController controller;
  
  private DefaultListModel classPathModel;
  
  Db4oPanel(Db4oOptionsPanelController controller) {
    this.controller = controller;
    initComponents();
    // TODO listen to changes in form fields and call controller.changed()
  }
  
  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  // <editor-fold defaultstate="collapsed" desc=" Erzeugter Quelltext ">//GEN-BEGIN:initComponents
  private void initComponents() {
    lblThreshold = new javax.swing.JLabel();
    txtThreshold = new javax.swing.JFormattedTextField();
    lblSuppress = new javax.swing.JLabel();
    txtSuppress = new javax.swing.JTextField();
    chkDelOnDefrag = new javax.swing.JCheckBox();
    lblClasspath = new javax.swing.JLabel();
    btnBrowseJar = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    lstClasspath = new javax.swing.JList();
    btnRemove = new javax.swing.JButton();
    lblServerTitle = new javax.swing.JLabel();
    lblServerPort = new javax.swing.JLabel();
    txtPort = new javax.swing.JTextField();
    lblUser = new javax.swing.JLabel();
    txtUser = new javax.swing.JTextField();
    lblPassword = new javax.swing.JLabel();
    txtPassword = new javax.swing.JPasswordField();

    org.openide.awt.Mnemonics.setLocalizedText(lblThreshold, "Threshold:");
    lblThreshold.setToolTipText("Sets the number of objects that are read at once");

    txtThreshold.setText("20");
    txtThreshold.setToolTipText("Sets the number of objects that are read at once");

    org.openide.awt.Mnemonics.setLocalizedText(lblSuppress, "Suppress:");

    txtSuppress.setText("com.db4o java net");
    txtSuppress.setToolTipText("Comma seoarated list of package that should be surpressed");

    org.openide.awt.Mnemonics.setLocalizedText(chkDelOnDefrag, "Delete on defragmentation");
    chkDelOnDefrag.setToolTipText("If checked, objects will be deleted on database defragementation");
    chkDelOnDefrag.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
    chkDelOnDefrag.setMargin(new java.awt.Insets(0, 0, 0, 0));

    org.openide.awt.Mnemonics.setLocalizedText(lblClasspath, "Classpath Settings: ");

    org.openide.awt.Mnemonics.setLocalizedText(btnBrowseJar, "Add Jar...");
    btnBrowseJar.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnBrowseJarActionPerformed(evt);
      }
    });

    jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));
    lstClasspath.setModel(new DefaultListModel());
    lstClasspath.setToolTipText("Custom classpath settings");
    jScrollPane1.setViewportView(lstClasspath);

    org.openide.awt.Mnemonics.setLocalizedText(btnRemove, "Remove");
    btnRemove.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnRemoveActionPerformed(evt);
      }
    });

    org.openide.awt.Mnemonics.setLocalizedText(lblServerTitle, "Embedded Server Settings:");

    org.openide.awt.Mnemonics.setLocalizedText(lblServerPort, "Port:");

    txtPort.setText("4488");
    txtPort.setToolTipText("Port used by embedded db4o server");

    org.openide.awt.Mnemonics.setLocalizedText(lblUser, "User:");

    txtUser.setToolTipText("User name for the embedded db4o server");

    org.openide.awt.Mnemonics.setLocalizedText(lblPassword, "Password:");

    txtPassword.setText("jPasswordField1");

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
        .addContainerGap()
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(layout.createSequentialGroup()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
              .add(lblThreshold)
              .add(lblSuppress))
            .add(17, 17, 17)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
              .add(txtSuppress, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
              .add(txtThreshold, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)))
          .add(chkDelOnDefrag)
          .add(lblClasspath)
          .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
              .add(btnRemove, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
              .add(btnBrowseJar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
          .add(lblServerTitle)
          .add(layout.createSequentialGroup()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
              .add(lblPassword)
              .add(lblServerPort)
              .add(lblUser))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 18, Short.MAX_VALUE)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
              .add(txtPassword)
              .add(txtUser)
              .add(txtPort, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE))))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
        .add(21, 21, 21)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(lblThreshold)
          .add(txtThreshold, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(lblSuppress)
          .add(txtSuppress, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .add(19, 19, 19)
        .add(chkDelOnDefrag)
        .add(23, 23, 23)
        .add(lblClasspath)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(layout.createSequentialGroup()
            .add(btnBrowseJar)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(btnRemove))
          .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 95, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .add(18, 18, 18)
        .add(lblServerTitle)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(lblServerPort)
          .add(txtPort, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(lblUser)
          .add(txtUser, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(lblPassword)
          .add(txtPassword, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .add(29, 29, 29))
    );
  }// </editor-fold>//GEN-END:initComponents

  private void btnBrowseJarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowseJarActionPerformed

    // Request file name from user
    JFileChooser fileChooser = new JFileChooser(".");
    fileChooser.setFileFilter(new ExtensionFileFilter("JAR", new String[] { "JAR" }));
    int status = fileChooser.showOpenDialog(null);
    if (status == JFileChooser.CANCEL_OPTION ) {
      return;
    }      
    File selectedFile = fileChooser.getSelectedFile();
    String fileName = selectedFile.getAbsolutePath();

    this.classPathModel.addElement(fileName);

  }//GEN-LAST:event_btnBrowseJarActionPerformed

  private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveActionPerformed

    int[] selected = this.lstClasspath.getSelectedIndices();
    for (int i = 0; i < selected.length; i++) {
      this.classPathModel.removeElementAt(selected[i]);
    }
      
  }//GEN-LAST:event_btnRemoveActionPerformed
    
    void load() {

      this.txtSuppress.setText(PreferencesUtil.getSuppressedPackages());
      this.txtThreshold.setText(""+PreferencesUtil.getThresholdValue());
      this.chkDelOnDefrag.setSelected(PreferencesUtil.isDeleteOnDefragmentation());

      this.txtPort.setText(PreferencesUtil.getServerPort());
      this.txtUser.setText(PreferencesUtil.getServerUser());
      this.txtPassword.setText(PreferencesUtil.getServerPassword());
      
      String[] libs = PreferencesUtil.getSpecifiedLibraries();
      this.classPathModel = new DefaultListModel();
      for (int i = 0; i < libs.length; i++) {
        this.classPathModel.addElement(libs[i]);
      }
      this.lstClasspath.setModel(this.classPathModel);
      
    }
    
    void store() {

      PreferencesUtil.setSuppressedPackages(this.txtSuppress.getText());
      PreferencesUtil.setThresholdValue(Integer.parseInt(this.txtThreshold.getText()));
      PreferencesUtil.setDeleteOnDefragmentation(this.chkDelOnDefrag.isSelected() );
      
      PreferencesUtil.setServerPort(this.txtPort.getText() );
      PreferencesUtil.setServerUser(this.txtUser.getText() );
      PreferencesUtil.setServerPassword( new String(this.txtPassword.getPassword()) );

      String classpathSettings = "";
      ListModel model = this.lstClasspath.getModel();
      for (int i = 0; i < model.getSize(); i++) {
        classpathSettings += model.getElementAt(i) + ";";
      }
      PreferencesUtil.setClasspathSettings(classpathSettings);
    }
    
    boolean valid() {
      // TODO check whether form is consistent and complete
      return true;
    }
    
  // Variablendeklaration - nicht modifizieren//GEN-BEGIN:variables
  private javax.swing.JButton btnBrowseJar;
  private javax.swing.JButton btnRemove;
  private javax.swing.JCheckBox chkDelOnDefrag;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JLabel lblClasspath;
  private javax.swing.JLabel lblPassword;
  private javax.swing.JLabel lblServerPort;
  private javax.swing.JLabel lblServerTitle;
  private javax.swing.JLabel lblSuppress;
  private javax.swing.JLabel lblThreshold;
  private javax.swing.JLabel lblUser;
  private javax.swing.JList lstClasspath;
  private javax.swing.JPasswordField txtPassword;
  private javax.swing.JTextField txtPort;
  private javax.swing.JTextField txtSuppress;
  private javax.swing.JFormattedTextField txtThreshold;
  private javax.swing.JTextField txtUser;
  // Ende der Variablendeklaration//GEN-END:variables
    
}
